package com.wly.frame.mvvm.base

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.wly.frame.R
import me.xdj.view.MultiStateView
import me.xdj.view.SimpleMultiStateView


/**
 * author: wangliyun
 * date: 2022/3/22
 * description:
 */
abstract class BaseActivity : AppCompatActivity() {

    protected lateinit var inflater: LayoutInflater
    private var viewBase: FrameLayout? = null

    protected var multiStateView: SimpleMultiStateView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        inflater = LayoutInflater.from(this)

        if (getLayoutRes() == 0 && getViewBinding() == null) {
            Log.i("wangly", "getLayoutRes -----")
            return
        }
        setContentView(R.layout.activity_base)
        viewBase = findViewById(R.id.viewBase)


        val viewRoot = if (getViewBinding() != null) {
            getViewBinding()?.root
        } else {
            View.inflate(this, getLayoutRes(), null);
        }
        initMultiStateView(viewRoot)
        viewBase?.addView(multiStateView)

        initView()
        initData()
    }

    private fun initMultiStateView(containerView: View?) {
        multiStateView = SimpleMultiStateView(this)
        val params = FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        ) //定义框架布局器参数

        if (configMultiStateView() == null) {
            Log.i("wangly", "configMultiStateView == null 使用默认配置")
            multiStateView?.addViewForStatus(MultiStateView.STATE_LOADING, R.layout.loading_view)
            multiStateView?.addViewForStatus(MultiStateView.STATE_EMPTY, R.layout.empty_view)
            multiStateView?.addViewForStatus(MultiStateView.STATE_FAIL, R.layout.error_view)

        }
        multiStateView?.setOnInflateListener { state, view ->

            if (state == MultiStateView.STATE_FAIL) {
                view.setOnClickListener {
                    onRetry()
                }
            }
        }
        multiStateView?.layoutParams = params
        multiStateView?.addView(containerView)
    }


    open fun onRetry() {

    }

    open fun configMultiStateView(): MultiStateView? {
        return null
    }


    protected open fun getViewBinding(): ViewBinding? {
        return null
    }

    abstract fun getLayoutRes(): Int

    abstract fun initView()
    abstract fun initData()


    fun showEmptyView() {
        multiStateView?.viewState = MultiStateView.STATE_EMPTY
    }

    fun showLoadingView() {
        multiStateView?.viewState = MultiStateView.STATE_LOADING
    }

    fun showFailView() {
        multiStateView?.viewState = MultiStateView.STATE_FAIL
    }

    fun showContentView() {
        multiStateView?.viewState = MultiStateView.STATE_CONTENT
    }


    override fun onDestroy() {
        super.onDestroy()
        //置空
        multiStateView?.setOnInflateListener(null)
    }
}